package com.showsu.editor.service.impl;

import cn.hutool.core.util.IdUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.showsu.editor.dao.TblXhsEditorAwemeRecordMapper;
import com.showsu.editor.entity.po.TblXhsEditorAwemeRecord;
import com.showsu.editor.entity.po.TblXhsEditorMediaRecord;
import com.showsu.editor.entity.vo.request.RequestVO;
import com.showsu.editor.enums.RowFlagEnum;
import com.showsu.editor.service.IXhsEditorAwemeRecordService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * xhs编辑器记录 服务类实现
 *
 * @author jph
 *  * @since 2024/9/19
 */
@Service
@Slf4j
public class XhsEditorAwemeRecordServiceImpl extends ServiceImpl<TblXhsEditorAwemeRecordMapper, TblXhsEditorAwemeRecord> implements IXhsEditorAwemeRecordService {

    @Autowired
    private TblXhsEditorAwemeRecordMapper mapper;


    @Override
    public int queryCountOfUser(Long userId) {
        if (userId == null) {
            return 0;
        }
        return mapper.selectCount(new QueryWrapper<TblXhsEditorAwemeRecord>().lambda()
                .eq(TblXhsEditorAwemeRecord::getFlag, RowFlagEnum.UnDelete.getCode())
                .eq(TblXhsEditorAwemeRecord::getUserId, userId));
    }

    @Override
    public int queryCountOfUserWithDate(Long userId, Date startDate, Date endDate) {
        if (userId == null || startDate == null || endDate == null) {
            return 0;
        }
        return mapper.selectCount(new QueryWrapper<TblXhsEditorAwemeRecord>().lambda()
                .eq(TblXhsEditorAwemeRecord::getUserId, userId)
                .ge(TblXhsEditorAwemeRecord::getCreateTime, startDate)
                .le(TblXhsEditorAwemeRecord::getCreateTime, endDate)
                .eq(TblXhsEditorAwemeRecord::getFlag, RowFlagEnum.UnDelete.getCode()));
    }

    @Override
    public List<TblXhsEditorAwemeRecord> queryByUserId(Long userId) {
        if (userId == null) {
            return null;
        }
        return mapper.selectList(new QueryWrapper<TblXhsEditorAwemeRecord>().lambda()
                .eq(TblXhsEditorAwemeRecord::getUserId, userId)
                .eq(TblXhsEditorAwemeRecord::getFlag, RowFlagEnum.UnDelete.getCode())
                .orderByDesc(TblXhsEditorAwemeRecord::getLastUpdate));
    }

    @Override
    public boolean saveOrUpdateRecord(RequestVO clientRequest, Long userId, TblXhsEditorAwemeRecord record){
        if(StringUtils.isBlank(record.getId())){
            record.setId(IdUtil.objectId());
        }
        record.setClientBrowser(clientRequest.getClientBrowser());
        record.setClientIp(clientRequest.getClientIp());
        record.setClientOrigin(clientRequest.getClientOrigin());
        record.setClientOs(clientRequest.getClientOs());
        record.setUserId(userId);
        return this.saveOrUpdate(record);
    }
}
